package com.beiben.work.hours.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.beiben.system.user.domain.BeiBenUser;
import com.beiben.system.user.mapper.BeiBenUserMapper;
import com.beiben.work.hours.domain.FileHoursDept;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.SysDeptMapper;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.beiben.work.hours.mapper.FileHoursMapper;
import com.beiben.work.hours.domain.FileHours;
import com.beiben.work.hours.service.IFileHoursService;

/**
 * 文件工时管理Service业务层处理
 * 
 * @author xgq
 * @date 2020-08-26
 */
@Service
public class FileHoursServiceImpl implements IFileHoursService 
{
    @Autowired
    private FileHoursMapper fileHoursMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private BeiBenUserMapper beiBenUserMapper;

    /**
     * 查询文件工时管理
     * 
     * @param fileId 文件工时管理ID
     * @return 文件工时管理
     */
    @Override
    public FileHours selectFileHoursById(Long fileId)
    {
        return fileHoursMapper.selectFileHoursById(fileId);
    }

    /**
     * 查询文件工时管理列表
     * 
     * @param fileHours 文件工时管理
     * @return 文件工时管理
     */
    @Override
    public List<FileHours> selectFileHoursList(FileHours fileHours)
    {
        return fileHoursMapper.selectFileHoursList(fileHours);
    }

    /**
     * 新增文件工时管理
     * 
     * @param fileHours 文件工时管理
     * @return 结果
     */
    @Override
    public int insertFileHours(FileHours fileHours)
    {
        fileHours.setCreateTime(DateUtils.getNowDate());
        int result =  fileHoursMapper.insertFileHours(fileHours);
        insertFileHoursDept(fileHours);
        return result;
    }

    /*
    * 保存关联组织机构信息
    * */
    public void insertFileHoursDept(FileHours fileHours){
        Long [] deptIds = fileHours.getDeptIds();
        for (Long deptId:deptIds){
            FileHoursDept fileHoursDept = new FileHoursDept();
            fileHoursDept.setDeptId(deptId);
            fileHoursDept.setFileId(fileHours.getFileId());
            fileHoursMapper.insertFileHoursDept(fileHoursDept);
        }
    }

    /**
     * 修改文件工时管理
     * 
     * @param fileHours 文件工时管理
     * @return 结果
     */
    @Override
    public int updateFileHours(FileHours fileHours)
    {
        fileHours.setUpdateTime(DateUtils.getNowDate());
        int result = fileHoursMapper.updateFileHours(fileHours);
        fileHoursMapper.deleteFileHoursDeptByfileId(fileHours.getFileId());
        insertFileHoursDept(fileHours);
        return result;
    }

    /**
     * 批量删除文件工时管理
     * 
     * @param fileIds 需要删除的文件工时管理ID
     * @return 结果
     */
    @Override
    public int deleteFileHoursByIds(Long[] fileIds)
    {
        fileHoursMapper.deleteFileHoursDeptByfileIds(fileIds);
        return fileHoursMapper.deleteFileHoursByIds(fileIds);
    }

    /**
     * 删除文件工时管理信息
     * 
     * @param fileId 文件工时管理ID
     * @return 结果
     */
    @Override
    public int deleteFileHoursById(Long fileId)
    {
        fileHoursMapper.deleteFileHoursDeptByfileId(fileId);
        return fileHoursMapper.deleteFileHoursById(fileId);
    }

    @Override
    public List<Long> selectFileHoursDeptByfileId(Long fileId) {
        return fileHoursMapper.selectFileHoursDeptByfileId(fileId);
    }
    /**
     * 查询组织结构下的工时
     *
     * @param fileHours 文件工时管理
     * @return 结果
     */
    @Override
    public List<FileHours> selectFileHoursDeptList(FileHours fileHours) {
        if(null!=fileHours.getUserId()){
            BeiBenUser user = beiBenUserMapper.selectUserById(fileHours.getUserId());
            if(null!=user){
                fileHours.setDeptId(user.getDeptId());
            }
        }
        SysDept sysDept = sysDeptMapper.selectDeptById(fileHours.getDeptId());
        String [] sdeptIds = null;
        Long[] lDeptIds = null;
        if(StringUtils.isNotEmpty(sysDept.getAncestors())){
            sdeptIds = sysDept.getAncestors().split(",");
            lDeptIds = (Long[]) ConvertUtils.convert(sdeptIds,Long.class);
        }else{
            lDeptIds = new Long[]{0l};
        }
        return fileHoursMapper.selectFileHoursDeptList(lDeptIds,fileHours.getDeptId());
    }
}
